<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Configuration
 *
 * @author MinhNguyen
 */

class Photo {
    //put your code here
    public $id;
    public $url;
    public $title;
    public $encode;
    public $description;
    public $hexcolor;
    public $alias;
    
    public static $instance;
    
    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }


    public function __get($name) {
        return $this->$name;
    }
    
    public function __set($name, $value) {
        $this->$name = $value;
    }   

    /**
     * It's constuctor of User with email
     */
    
    public  function __construct() {
        //allocate your stuff
    }

    
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM photos WHERE id = '$id'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->url = $rows["url"];
                $instance->encode = $rows["encode"];
                $instance->title = $rows["title"];
                $instance->description = $rows["description"];
                $instance->alias = $rows['id'].'-'.str_replace(' ', '-', $rows['title']);
                return $instance;
            }
            else {
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }  
    
    public static function withEncode($encode) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM photos WHERE encode = '$encode'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->url = $rows["url"];
                $instance->encode = $rows["encode"];
                $instance->title = $rows["title"];
                $instance->description = $rows["description"];
                return $instance;
            }
            else {
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
    
    public static function withData($url, $title, $description, $hexcolor = null) {
        $instance = new self();
        $instance->url = $url;
        $instance->title = $title;
        $instance->description = $description;
        $instance->hexcolor = $hexcolor;
        return $instance;
    }
    
    public static function addPhoto($photo, $avatar = false){
        $db = Database::getInstance();
        $id = $db->getID("photos", "id");
        $url = $photo->url;
        $encode = md5($id.'_'.$url);
        $title = $photo->title;
        $description = $photo->description;
        $query = "INSERT INTO photos (id, url, encode, title, description) VALUES 
                ('$id', '$url', '$encode', '$title', '$description')";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if($avatar){
                User::updateColumn("avatarid", $id);
                Photo::addPhotoData($id, 0, $photo->hexcolor);
            }
            return $id;
        }else
            return false;
    }
    
    public static function deletePhoto($photoId, $avatar = false) {
        if(isset($_SESSION['userid'])) {
            $db = Database::getInstance();
            $photo = Photo::withId($photoId);
            if(unlink($photo->url)){
                if($avatar){
                    User::updateColumn("avatarid", "0");
                }else {
                    Status::deleteStatus(Status::getStatusId($photoId));
                }
            }
            $query = "DELETE FROM photos WHERE id = '$photoId'"; 
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                Photo::deletePhotoData($photoId);
                return "true";
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView, 
                        $result->returnCode, $result->message);
                $controller->returnError();
                return "false";
            }
        }
        else {
            return "false";
        }
    }
    
     public static function deletePhotoColors($photoId){
        $db = Database::getInstance();
        $query = "DELETE FROM photocolors WHERE photoid = '$photoId'"; 
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            $query = "DELETE FROM photos WHERE id = '$photoId'"; 
            $result = $db->query($query);
            if ($result->returnCode == 200) {
                return "true";
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView, 
                        $result->returnCode, $result->message);
                $controller->returnError();
                return "false";
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return "false";
        }
    }
    
    public static function addPhotoData($photoId, $statusId, $hexColor = null){
        $db = Database::getInstance();
        $id = $db->getID("photodata", "id");
        $photoid = $photoId;
        $statusid = $statusId;
        $hexcolor = $hexColor;
        $query = "INSERT INTO photodata (id, photoid, statusid, hexcolor) VALUES 
                ('$id', '$photoid', '$statusid', '$hexcolor')";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            return true;
        }else
            return false;
    }
    
    public static function deletePhotoData($photoId){
        $db = Database::getInstance();
        $query = "DELETE FROM photodata WHERE photoid = '$photoId'"; 
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            Photo::deletePhotoColors($photoId);
            return "true";
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return "false";
        }
    }
    
    public static function getPhotoId($statusid){
        $db = Database::getInstance();
        $query = "SELECT * FROM photodata WHERE statusid='$statusid'";
        $result = $db->query($query);
        if($result->returnCode == 200){
            if($result->dataTable->numOfRow == 1){
                $row = $result->dataTable->rows[0];
                return $row["photoid"];
            }else{
                return NULL;
            }
        }else{
            return NULL;
        }    
    }
}
?>
